package com.example.timetable;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;

import java.util.HashMap;
import java.util.Map;

import com.example.timetable.method.Database;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemSelected;

@Route(path="/TimeTable/TimeTableCreate")

public class TimeTableCreate extends AppCompatActivity  {
    private  String coursename=null,courseroom=null,courseteacher=null,weekbegain="第1周",weekend="第2周", courseday="周一",jiebegain="第一节",jieend="第二节",which="连续周";
    private  String color;
    private  int weekbegainnum,weekendnum,jiebegainnum,jieendnum,weekday=-2;
    int colorId;
    int CheckItem=0;
    Boolean isupdate=false;
    private  int i;
    int id=1;

    private Database database=new Database(this);

    Map<String,String>time=new HashMap<String, String>();
    Map<String,Integer> week =new HashMap<String, Integer>();
    Map<String,Integer> jie =new HashMap<String, Integer>();

    RadioGroup radioGroup2;
    RadioButton radioButton1;
    Button choiceColor;

    EditText Coursename;
    EditText Courseroom;
    EditText Courseteacher;

    @BindView(R.id.weekbegain)
    Spinner spinner1;
    @BindView(R.id.weekend)
    Spinner spinner2;
    @BindView(R.id.courseday)
    Spinner spinner3;
    @BindView(R.id.jiebegain)
    Spinner spinner4;
    @BindView(R.id.jieend)
    Spinner spinner5;
    @BindView(R.id.which)
    RadioGroup radioGroup1;
    @BindView(R.id.runweek)
    RadioButton which1;
    @BindView(R.id.dostweek)
    RadioButton which2;
    @BindView(R.id.dualweek)
    RadioButton which3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_table_create);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        ButterKnife.bind(this);
        radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
                selectRadioButton();
            }
            private void selectRadioButton() {
                RadioButton rb = findViewById(radioGroup1.getCheckedRadioButtonId());
                which=rb.getText().toString();
            }
        });
        color=String.valueOf(ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable1));
        radioButton1=findViewById(radioGroup1.getCheckedRadioButtonId());
        choiceColor=findViewById(R.id.choicecolor);
        Coursename=findViewById(R.id.coursename);
        Courseroom=findViewById(R.id.courseroom);
        Courseteacher=findViewById(R.id.courseteacher);

        time.put("第一节","8.00");time.put("第二节","9.00");time.put("第三节","10.10");time.put("第四节","11.10");
        time.put("第五节","12.10");time.put("第六节","13.10");time.put("第七节","14.30");time.put("第八节","15.30");
        time.put("第九节","16.30");time.put("第十节","17.30");time.put("第十一节","19.10");time.put("第十二节","20.10");
        time.put("第十三节","21.10");

        week.put("第1周",0); week.put("第2周",1); week.put("第3周",2); week.put("第4周",3); week.put("第5周",4);
        week.put("第6周",5); week.put("第7周",6); week.put("第8周",7); week.put("第9周",8); week.put("第10周",9);
        week.put("第11周",10); week.put("第12周",11); week.put("第13周",12); week.put("第14周",13); week.put("第15周",14);
        week.put("第16周",15); week.put("第17周",16); week.put("第18周",17); week.put("第19周",18); week.put("第20周",19);

        jie.put("第一节",0);jie.put("第二节",1);jie.put("第三节",2);jie.put("第四节",3);jie.put("第五节",4);jie.put("第六节",5);
        jie.put("第七节",6);jie.put("第八节",7);jie.put("第九节",8);jie.put("第十节",9);jie.put("第十一节",10);jie.put("第十二节",11);
        jie.put("第十三节",12);

        colorId=ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable1);
        Intent intent=getIntent();
        try{
            weekday=Integer.parseInt(intent.getStringExtra("week"))-1;
            jiebegainnum=Integer.parseInt(intent.getStringExtra("jies"))*2-1-1;
            System.out.println("开始节数"+jiebegainnum);
        }
        catch (Exception e){
            System.out.println(e);
        }
        if(weekday!=-2) {
            isupdate=true;
            Database db = new Database(this);
            Cursor cursors = db.select(weekday);
            if (cursors.getCount() != 0) {
                //               count=0;    //记录当前表中课程数量
                for(int z=0;z<cursors.getCount();z++){
                    cursors.moveToNext();
                    if(jiebegainnum==jie.get(cursors.getString(cursors.getColumnIndex("jieshus")))){
                        break;
                    }

                }
                System.out.println(jiebegainnum+"=="+jie.get(cursors.getString(cursors.getColumnIndex("jieshus"))));
                //                  count++;
                //根据索引值获取当前行数所保存的课程的数据
                try{
                    id=Integer.parseInt(cursors.getString(cursors.getColumnIndex("_id")));
                    coursename = cursors.getString(cursors.getColumnIndex("classes"));
                    courseroom = cursors.getString(cursors.getColumnIndex("location"));
                    courseteacher = cursors.getString(cursors.getColumnIndex("teacher"));
                    weekbegain =cursors.getString(cursors.getColumnIndex("zhoushus"));
                    weekend = cursors.getString(cursors.getColumnIndex("zhoushue"));
                    jiebegain = cursors.getString(cursors.getColumnIndex("jieshus"));
                    jieend = cursors.getString(cursors.getColumnIndex("jieshue"));
                    which = cursors.getString(cursors.getColumnIndex("which"));
                    color = cursors.getString(cursors.getColumnIndex("color"));
                }catch (Exception e){
                    System.out.println(e);
                }


            }
            Coursename.setText(coursename);
            Courseroom.setText(courseroom);
            Courseteacher.setText(courseteacher);
            spinner1.setSelection(week.get(weekbegain));
            spinner2.setSelection(week.get(weekend));
            spinner3.setSelection(weekday);
            spinner4.setSelection(jie.get(jiebegain));
            spinner5.setSelection(jie.get(jieend));
            if (which.equals("连续周")){
                which1.setChecked(true);
            }
            else if (which.equals("单周"))
                which2.setChecked(true);
            else
                which3.setChecked(true);
            choiceColor.setBackgroundColor(Integer.parseInt(color));
            System.out.println(Integer.parseInt(color));
            final int t=ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable3);
            if(Integer.parseInt(color)==ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable1))
                CheckItem=0;
            else if (Integer.parseInt(color)==ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable2))
                CheckItem=1;
            else if (Integer.parseInt(color)==ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable3))
                CheckItem=2;
            else if (Integer.parseInt(color)==ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable4))
                CheckItem=3;
            else if (Integer.parseInt(color)==ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable5))
                CheckItem=4;

        }
    }

    @OnClick(R.id.choicecolor)//设置选择颜色对话框
    public void choicecolor() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择颜色");
        final String[] items = new String[]{"橙色", "紫色", "蓝色", "红色", "绿色"};
        builder.setSingleChoiceItems(items, CheckItem, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                CheckItem=which;
                switch (which){
                    case 0:
                        colorId=ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable1);
                        color="courseTable1";
                        break;
                    case 1:
                        colorId=ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable2);
                        color="courseTable2";
                        break;
                    case 2:
                        colorId=ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable3);
                        color="courseTable3";
                        break;
                    case 3:
                        colorId=ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable4);
                        color="courseTable4";
                        break;
                    case 4:
                        colorId=ContextCompat.getColor(TimeTableCreate.this,R.color.courseTable5);
                        color="courseTable5";
                        break;
                }
                choiceColor.setBackgroundColor(colorId);
                dialog.dismiss();
            }
        });

        builder.show();

    }

    @OnClick(R.id.savecourse)
    //设置监听事件保存数据
    public void savecourse(){
        coursename=Coursename.getText().toString();//获取课程名称
        courseroom=Courseroom.getText().toString();//获取上课教室
        courseteacher=Courseteacher.getText().toString();//获取上课老师
        which=radioButton1.getText().toString();//获取上课单双周或者连续周
        System.out.println(2);
        if(coursename.trim().isEmpty()||courseroom.trim().isEmpty()||courseteacher.trim().isEmpty())
            Toast.makeText(this,"您有未输入的输入框",Toast.LENGTH_LONG).show();
        else if(weekendnum<weekbegainnum)
            Toast.makeText(this,"您选择的周数有误",Toast.LENGTH_LONG).show();
        else if(jieendnum<jiebegainnum)
            Toast.makeText(this,"您选择的节数有误",Toast.LENGTH_LONG).show();
        else {
            if(isupdate==false)
                database.insert(i,coursename, courseroom, courseteacher, weekbegain, weekend, jiebegain, jieend, time.get(jiebegain), time.get(jieend), which, String.valueOf(colorId));
            else{
                database.update(i,id ,coursename, courseroom, courseteacher, weekbegain, weekend, jiebegain, jieend, time.get(jiebegain), time.get(jieend), which, String.valueOf(colorId));
            }

            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            Toast.makeText(this, "已储存", Toast.LENGTH_LONG).show();
        }

    }
    @OnClick(R.id.deletecourse)
    public void deletecourse(){
        if(isupdate){
            database.delete(i,id);
            Intent intent=new Intent(this,MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        else
            Toast.makeText(this,"未选择课程",Toast.LENGTH_LONG).show();
    }

    @OnItemSelected(R.id.weekbegain)//获取开始周
    public void onItemSelected1(int position) {
        weekbegainnum=position;
        position++;
        weekbegain = "第" + position + "周";
    }

    @OnItemSelected(R.id.weekend)//获取结束周
    public void onItemSelected2(int position) {
        weekendnum=position;
        position++;
        weekend = "第" + position + "周";
    }

    @OnItemSelected(R.id.courseday)//获取选择上课的周几
    public void onItemSelected3(int position) {
        courseday = spinner3.getItemAtPosition(position).toString();
        switch (courseday) {
            case "周一":
                i = 0;
                break;
            case "周二":
                i = 1;
                break;
            case "周三":
                i = 2;
                break;
            case "周四":
                i = 3;
                break;
            case "周五":
                i = 4;
                break;
            case "周六":
                i = 5;
                break;
            case "周日":
                i = 6;
                break;
        }

    }

    @OnItemSelected(R.id.jiebegain)//获取下拉框开始节数
    public void onItemSelected4(int position) {
        jiebegainnum=position;
        switch (position) {
            case 0:
                jiebegain = "第一节";
                break;
            case 1:
                jiebegain = "第二节";
                break;
            case 2:
                jiebegain = "第三节";
                break;
            case 3:
                jiebegain = "第四节";
                break;
            case 4:
                jiebegain = "第五节";
                break;
            case 5:
                jiebegain = "第六节";
                break;
            case 6:
                jiebegain = "第七节";
                break;
            case 7:
                jiebegain = "第八节";
                break;
            case 8:
                jiebegain = "第九节";
                break;
            case 9:
                jiebegain = "第十节";
                break;
            case 10:
                jiebegain = "第十一节";
                break;
            case 11:
                jiebegain = "第十二节";
                break;

            case 12:
                jiebegain="第十三节";
                break;

        }

    }

    @OnItemSelected(R.id.jieend)//获取下拉框结束节数
    public void onItemSelected5(int position) {
        jieendnum=position;
        switch (position) {
            case 0:
                jieend = "第一节";
                break;
            case 1:
                jieend = "第二节";
                break;
            case 2:
                jieend = "第三节";
                break;
            case 3:
                jieend = "第四节";
                break;
            case 4:
                jieend = "第五节";
                break;
            case 5:
                jieend = "第六节";
                break;
            case 6:
                jieend = "第七节";
                break;
            case 7:
                jieend = "第八节";
                break;
            case 8:
                jieend = "第九节";
                break;
            case 9:
                jieend = "第十节";
                break;
            case 10:
                jieend = "第十一节";
                break;
            case 11:
                jieend = "第十二节";
                break;

            case 12:
                jieend="第十三节";
                break;

        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            Intent intent=new Intent(this,MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        return super.onKeyUp(keyCode, event);
    }
}
